<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/20 9:21
 * Modified Time: 2019/7/20 9:21
 * Version: 2.0
 */

namespace App\Modules\System\Services;


use App\Exceptions\AllValidatorException;
use App\Lib\Utilities;
use App\Modules\Employee\Repositories\EmployeeRepository;
use App\Modules\System\Repositories\RoleRepository;
use Flower\Core\Repository\Eloquent\BaseService;

class CommonService extends BaseService
{
    protected $employeeRepository;
    private $roleRepository;

    public function __construct(EmployeeRepository $employeeRepository, RoleRepository $roleRepository)
    {
        $this->employeeRepository = $employeeRepository;
        $this->roleRepository = $roleRepository;
    }


    /**
     * 发送短信验证码
     * @param $mobile 手机号
     * @param $scene 场景值 0:注册 1:小程序领取会员卡 2:pc端店长登录转移会员卡 3:找回密码
     * @throws AllValidatorException
     */

    public function sendSms($mobile, $scene)
    {
        //验证手机号格式
        $input = ['mobile' => $mobile];

        $inputRules = [
            'mobile' => 'numeric|between:10000000000,19999999999'
        ];
        $validator = \Validator::make($input, $inputRules);

        if (!$validator->passes()) {
            throw new AllValidatorException(['msg' => '手机号格式错误']);
        }


        switch ($scene) {
            case 0:
                if ($this->employeeRepository->checkUserMobileExist($mobile)) {
                    throw new AllValidatorException(['msg' => '该手机号码已被注册']);
                }
                break;
            case 1:
                break;
            case 2:
                break;
            case 3:
                if (!$this->employeeRepository->checkUserMobileExist($mobile)) {
                    throw new AllValidatorException(['msg' => '该手机号码未注册']);
                }
                break;
            case 4:
                break;
            case 5:
                break;
            default:
                throw new AllValidatorException(['msg' => '场景值错误']);
        }


        Utilities::sendSmsCode($mobile, $scene);
    }

    /*
     * 验证短信验证码
     * $mobile:手机号码
     * $scene:场景值 0:注册 1:小程序领取会员卡 2:pc端店长登录转移会员卡 3:找回密码
     * $code:验证码
     */
    public function checkSmsCode($mobile, $scene, $code)
    {

        return Utilities::checkSmsCode($mobile, $scene, $code);
    }

    /**
     * 获取是否有看理发师和店长面板权限
     * @param $ids
     * @return mixed
     */
    public function getBossBarberControl($ids)
    {
        return $this->roleRepository->getBossBarberControl($ids);
    }

	/**
	 * 获取角色列表
	 * @return mixed
	 */
	public function getRoleList(){
    	return $this->roleRepository->getList();
    }
}